package com.zhongge.observer;

import java.util.List;
import java.util.ArrayList;

/**
 * @ClassName Product
 * @Description TODO 被观察者(主题)：商品
 * @Author zhongge
 * @Version 1.0
 */
public class Product {
    //名字
    private String name;
    //价格
    private double price;

    //存储订阅的会员
    private List<Member> members = new ArrayList<>();

    //构造函数
    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    //注册会员
    public void registerMember(Member member) {
        members.add(member);
    }

    //移除会员
    public void removeMember(Member member) {
        members.remove(member);
    }

    //商品数据变化时候，通知所有会员
    public void notifyMembers() {
        for (Member m : members) {
            m.update(name, price);
        }
    }

    //修改商品名称，触发通知
    public void setName(String name) {
        this.name = name;
        notifyMembers();
    }

    //修改商品价格，触发通知
    public void setPrice(double price) {
        this.price = price;
        notifyMembers();
    }
}
